﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SaveFileDialog = Microsoft.Win32.SaveFileDialog;

namespace ConfigCreate.Controls
{
    /// <summary>
    /// CreateControlSaveFile.xaml 的交互逻辑
    /// </summary>
    public partial class CreateControlSaveFile : System.Windows.Controls.UserControl
    {
        /// <summary>
        /// 保存文件选择事件
        /// </summary>
        public event EventHandler<EventArgs>? SelectedFileChanged;

        /// <summary>
        /// 
        /// </summary>
        public CreateControlSaveFile()
        {
            InitializeComponent();
        }


        #region 属性
        /// <summary>
        /// 选择文件路径
        /// </summary>
        public string SelectedFileName
        {
            get { return (string)GetValue(SelectedFileNameProperty); }
            set { SetValue(SelectedFileNameProperty, value); }
        }
        /// <summary>
        /// 选择文件路径
        /// </summary>
        public static readonly DependencyProperty SelectedFileNameProperty = DependencyProperty.Register("SelectedFileName", typeof(string), typeof(CreateControlSaveFile), new PropertyMetadata(string.Empty));

        /// <summary>
        /// 对话框标题
        /// </summary>
        public string DialogTitle
        {
            get { return (string)GetValue(DialogTitleProperty); }
            set { SetValue(DialogTitleProperty, value); }
        }
        /// <summary>
        /// 对话框标题
        /// </summary>
        public static readonly DependencyProperty DialogTitleProperty = DependencyProperty.Register("DialogTitle", typeof(string), typeof(CreateControlSaveFile), new PropertyMetadata("请选择文件"));

        /// <summary>
        /// 对话框过滤
        /// </summary>
        public string DialogFilter
        {
            get { return (string)GetValue(DialogFilterProperty); }
            set { SetValue(DialogFilterProperty, value); }
        }
        /// <summary>
        /// 对话框过滤
        /// </summary>
        public static readonly DependencyProperty DialogFilterProperty = DependencyProperty.Register("DialogFilter", typeof(string), typeof(CreateControlSaveFile), new PropertyMetadata("所有文件|*.*"));


        /// <summary>
        /// 其中文本框是否为只读模式
        /// </summary>
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }
        /// <summary>
        /// 其中文本框是否为只读模式
        /// </summary>
        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(CreateControlSaveFile), new PropertyMetadata(false));

        /// <summary>
        /// 文件的默认扩展名
        /// </summary>
        public string DefaultExt
        {
            get { return (string)GetValue(DefaultExtProperty); }
            set { SetValue(DefaultExtProperty, value); }
        }
        /// <summary>
        /// 文件的默认扩展名
        /// </summary>
        public static readonly DependencyProperty DefaultExtProperty = DependencyProperty.Register("DefaultExt", typeof(string), typeof(CreateControlSaveFile), new PropertyMetadata(null));

        /// <summary>
        /// 打开对话框的初始目录
        /// </summary>
        public string InitialDirectory
        {
            get { return (string)(GetValue(InitialDirectoryProperty)); }
            set { SetValue(InitialDirectoryProperty, value); }
        }
        /// <summary>
        /// 打开对话框的初始目录
        /// </summary>
        public static readonly DependencyProperty InitialDirectoryProperty = DependencyProperty.Register("InitialDirectory", typeof(string), typeof(CreateControlSaveFile), new PropertyMetadata(null));
        #endregion

        private void OpenFile_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Title = DialogTitle;
            sf.Filter = DialogFilter;
            if (!string.IsNullOrEmpty(InitialDirectory))
            {
                sf.InitialDirectory = InitialDirectory;
            }
            else if (!string.IsNullOrEmpty(SelectedFileName))
            {
                FileInfo finfo = new FileInfo(SelectedFileName);
                if (finfo.Directory?.Exists == true)
                    sf.InitialDirectory = finfo.DirectoryName;

            }
            sf.FileName = SelectedFileName;
            if (sf.ShowDialog() == true)
            {
                SelectedFileName = sf.FileName;
                SelectedFileChanged?.Invoke(this, EventArgs.Empty);
            }
        }
    }
}
